package com.dna.controller;

import java.util.Map;
import java.util.Set;

import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dna.entity.UserEntity;
import com.dna.service.UserService;
import com.dna.utils.Assert;
import com.dna.utils.Constant.UserState;
import com.dna.utils.Query;
import com.dna.utils.R;
import com.dna.utils.ShiroUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@RestController
@RequestMapping("/user")
@Api("用户信息")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("info")
    @ApiOperation(value = "获取当前用户")
    public R info() {
        return R.ok().put("user", ShiroUtils.getUserEntity());
    }

    @GetMapping("info/{id}")
    @ApiOperation(value = "查询用户")
    public R info(@PathVariable("id") Long id) {
        return R.ok().put("user", userService.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询用户列表")
    public R list(@RequestParam Map<String, Object> map) {

        Query<UserEntity> query = new Query<UserEntity>(map, UserEntity.class);

        PageInfo<UserEntity> pageInfo = userService.queryPageList(query);

        return R.ok(pageInfo.getTotal(), pageInfo.getList());

    }

    @PostMapping
    @ApiOperation(value = "保存用户")
    public R saveUser(UserEntity ue) {

        UserEntity userEntity = userService.queryOne(ue);
        if (userEntity != null) {
            return R.error(ue.getUsername() + " 已存在!");
        }
        String password = new Sha256Hash("123456").toHex();
        ue.setPassword(password);
        userService.save(ue);

        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "更新用户")
    public R updateUser(UserEntity ue) {
        userService.update(ue);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除用户")
    public R deleteUser(@RequestBody Set<Long> ids) {
        if (ids.contains(1L)) {
            return R.error("系统管理员不能删除");
        }

        if (ids.contains(ShiroUtils.getUserId())) {
            return R.error("当前用户不能删除");
        }

        UserEntity ue = new UserEntity();
        ue.setState(UserState.DELETE.getValue());

        Example example = new Example(UserEntity.class);
        Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        userService.delete(ue, example);
        return R.ok();
    }

    /**
     * 修改登录用户密码
     */
    @PutMapping("updatePassword")
    @ApiOperation(value = "修改密码")
    public R password(String password, String newPassword) {
        Assert.isBlank(newPassword, "新密码不为能空");

        password = new Sha256Hash(password).toHex();
        newPassword = new Sha256Hash(newPassword).toHex();

        int count = userService.updatePassword(ShiroUtils.getUserId(), password, newPassword);
        if (count == 0) {
            return R.error("原密码不正确");
        }

        ShiroUtils.logout();

        return R.ok();
    }
}